/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import cn.itlaobing.exception.BusinessException;
import cn.itlaobing.util.IRowMapper;

/** 
 * ClassName: DBHelper <br/> 
 * Function:所有类的父类<br/> 
 * date: 2017年11月1日 上午10:01:52 <br/> 
 * 
 * @author 王博 
 * @version  
 * @since JDK 1.8 
 */
public class DBHelper<T> {
	private static String DRIVER;
	private static String URL;
	private static String USER;
	private static String PWD;
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	
	static {
		InputStream is=DBHelper.class.getClassLoader().getResourceAsStream("db.properties");
		Properties pro=new Properties();
		try {
			pro.load(is);
		} catch (IOException e) {
			try {
				throw new BusinessException(e.getMessage());
			} catch (BusinessException e1) {
				e1.printStackTrace();
			}
		}
		DRIVER=pro.getProperty("jdbc.driver");
		URL=pro.getProperty("jdbc.url");
		USER=pro.getProperty("jdbc.user");
		PWD=pro.getProperty("jdbc.pwd");
	}
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			try {
				throw new BusinessException(e.getMessage());
			} catch (BusinessException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	/** 
	 * openConnection:(连接数据库). <br/>
	 * 
	 * @author 王博 
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	private void openConnection() throws SQLException {
		if(conn==null || conn.isClosed()) {
			conn=DriverManager.getConnection(URL,USER,PWD);
		}
	}
	
	/** 
	 * executeUpdate:(执行增删改操作). <br/>
	 * 
	 * @author 王博 
	 * @param sql
	 * @param obj
	 * @return
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	public int executeUpdate(String sql,Object...obj) throws SQLException { 
		openConnection();
		ps=conn.prepareStatement(sql);
		prepareStat(obj);
		return ps.executeUpdate();
	}

	/** 
	 * prepareStat:(给占位符赋值). <br/>
	 * 
	 * @author 王博 
	 * @param obj 
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	private void prepareStat(Object[] obj) throws SQLException {
		if(obj!=null && obj.length!=0) {
			for(int i=0;i<obj.length;i++) {
				ps.setObject(i+1, obj[i]);
			}
		}
	}
	
	/** 
	 * executeQuery:(执行查询操作). <br/>
	 * 
	 * @author 王博 
	 * @param sql
	 * @param obj
	 * @return
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	private ResultSet queryAssist(String sql,Object...obj) throws SQLException {
		openConnection();
		ps=conn.prepareStatement(sql);
		prepareStat(obj);
		return ps.executeQuery();
	}
	
	/** 
	 * executeQuery:(调用查询操作，并返回结果). <br/>
	 * 
	 * @author 王博 
	 * @param sql
	 * @param rm
	 * @param obj
	 * @return
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	public List<T> executeQuery(String sql,IRowMapper<T> rm,Object...obj) throws SQLException{
		rs=queryAssist(sql,obj);
		List<T> list=new ArrayList<T>();
		while(rs.next()) {
			T t=rm.rowMapper(rs);
			list.add(t);
		}
		return list;
	}
	
	/** 
	 * executeQueryID:(单查). <br/>
	 * 
	 * @author 王博 
	 * @param sql
	 * @param rm
	 * @param obj
	 * @return
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	public T executeQueryID(String sql,IRowMapper<T> rm,Object...obj) throws SQLException {
		rs=queryAssist(sql,obj);
		T t=null;
		if(rs.next()) {
			t=rm.rowMapper(rs);
		}
		return t;
	}
	
	/** 
	 * executeScalar:(执行聚合函数查询). <br/>
	 * 
	 * @author 王博 
	 * @param sql
	 * @param obj
	 * @return
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	public Object executeScalar(String sql,Object...obj) throws SQLException {
		rs=queryAssist(sql,obj);
		Object value=null;
		if(rs.next()) {
			value=rs.getObject(1);
		}
		return value;
	}
	
	
	/** 
	 * close:(关闭资源). <br/>
	 * 
	 * @author 王博 
	 * @throws SQLException 
	 * @since JDK 1.8 
	 */  
	public void close() throws SQLException {
		if(rs!=null)rs.close();
		if(ps!=null)ps.close();
		if(conn!=null || !conn.isClosed())conn.close();
	}
	
}
